package com.backend.modules.task.convert;

import com.backend.modules.task.entity.TaskSubmission;
import com.backend.modules.task.entity.TaskSubmissionFile;
import com.backend.modules.task.vo.TaskSubmissionFileVO;
import com.backend.modules.task.vo.TaskSubmissionVO;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务提交记录对象转换器
 */
public class TaskSubmissionConvert {
    
    /**
     * 将提交记录实体转换为VO对象
     */
    public static TaskSubmissionVO convert(TaskSubmission submission) {
        if (submission == null) {
            return null;
        }
        
        TaskSubmissionVO vo = new TaskSubmissionVO();
        BeanUtils.copyProperties(submission, vo);
        
        // 设置任务信息
        if (submission.getTask() != null) {
            vo.setTaskId(submission.getTask().getId());
            vo.setTaskName(submission.getTask().getName());
        }
        
        // 设置学生信息
        if (submission.getStudent() != null) {
            vo.setStudentId(submission.getStudent().getId());
            vo.setStudentName(submission.getStudent().getRealName());
          vo.setStudentNo(submission.getStudent().getUserCode());
        }
        
        // 设置文件列表
        List<TaskSubmissionFileVO> fileVOs = submission.getFiles().stream()
                .map(file -> {
                    TaskSubmissionFileVO fileVO = new TaskSubmissionFileVO();
                    fileVO.setId(file.getId());
                    fileVO.setFileName(file.getFileName());
                    fileVO.setFileUrl(file.getFileUrl());
                    fileVO.setFileSize(file.getFileSize());
                    fileVO.setFileType(file.getFileType());
                    fileVO.setSort(file.getSort());
                    return fileVO;
                })
                .collect(Collectors.toList());
        vo.setFiles(fileVOs);
        
        // 设置是否逾期提交
        if (submission.getSubmitTime() != null && submission.getTask() != null) {
            vo.setIsLate(submission.getSubmitTime().isAfter(submission.getTask().getDeadline()));
        }
        
        return vo;
    }
    
    /**
     * 将提交记录实体列表转换为VO列表
     */
    public static List<TaskSubmissionVO> convertList(List<TaskSubmission> list) {
        return list.stream()
                .map(TaskSubmissionConvert::convert)
                .collect(Collectors.toList());
    }
} 